// Select stocks which satisfy Rule 3.2 //
use ../dta/daily_tot.dta, clear

drop Clsprc_sh Clsprc_sz Clsprc_zx Clsprc_inv Hpr_sh Hpr_sz Hpr_zx Hpr_inv Lpr_sh Lpr_sz Lpr_zx Lpr_inv change_sh change_sz change_zx change_inv turnover_sh turnover_sz turnover_zx turnover_inv
drop if y~=2014
drop if m>8

sort code date
gen float com_day=mdy(08,31,2014)
format %td com_day
gen day= com_day-List_date
sort List_date
//(1) Trading period longer than 3 months//
count if day==.
gen d_list=1 if day>90
replace d_list=0 if day<=90
sort code date  
// period_d

//(2) margin buy: Tradable shares >= 100 million or tradable market value >= 500 million//
gen float_shares= float_cap/ price
gen d_cap=1 if float_shares>=100000 | float_cap>=500000 
// float_cap, 1 unit=1,000 RMB
replace d_cap=0 if float_shares<100000 & float_cap<500000
 // daily_d, maximum needed

//(3) Number of shareholders >= 4,000//
gen d_holder=1 if no_holders>=4000 & no_holders~=.
replace d_holder=0 if no_holders<4000 | no_holders==.
//daily_d, maximum needed

// First chose window 2014. 6-8 //
drop if m<6
sort code date
bysort code: egen avg_turnover=mean( turnover_float )
bysort code: egen avg_turnover_base=mean( base_turn )
bysort code: egen avg_change=mean(abs(change))
bysort code: egen avg_change_base=mean(abs(base_change) )
bysort code: egen avg_dollarvolume =mean( volume_value )
 // 1 unit: 1 RMB
bysort code: egen max_hpr=max( price )
bysort code: egen min_lpr=min( price )
bysort code: egen max_hpr_base=max( base_clsprc )
bysort code: egen min_lpr_base=min( base_clsprc )

gen vol= 2*(max_hpr- min_lpr)/( max_hpr + min_lpr )
gen vol_base= 2*(max_hpr_base- min_lpr_base)/( max_hpr_base+ min_lpr_base )


//(4) In past 3 months, the following cases never happen
//c. Average daily turnover < 15% of index daily turnover and daily trading dollar volume < 50 million//
gen d_turn=0 if avg_turnover*100< avg_turnover_base*0.15 & avg_dollarvolume<50000000
replace d_turn=1 if avg_turnover*100>=avg_turnover_base*0.15 | avg_dollarvolume>=50000000 
//period_d
//a. Average daily price change's deviation from index > 4%//
gen d_change=0 if abs(avg_change- avg_change_base)>4
replace d_change=1 if  abs(avg_change- avg_change_base)<=4 
//period_d
//b. Volatility is larger than 5 times as index volatility//
gen d_vol=0 if vol>= 5*vol_base
replace d_vol=1 if vol<5*vol_base 
//period_d

//(5) Non-tradable share reform completed//
gen refday= com_day- Reform_date
gen d_ref=1 if year(List_date)>=2006 
replace d_ref=1 if refday>0 & refday~=.
replace d_ref=0 if d_ref ==. 
//period_d

//(6) No special treated records//
gen d_sts=1 if trading_status==1|trading_status==4|trading_status==10
replace d_sts=0 if trading_status==2| trading_status==3| trading_status==6 
//daily_d, minimum needed

sort code date
bysort code: egen d_cap_max=max( d_cap)
bysort code: egen d_holder_max=max( d_holder)
bysort code: egen d_sts_min=min( d_sts)

gen ind=( d_list+ d_cap_max+ d_holder_max+ d_turn+d_change +d_vol +d_ref+d_sts_min)/8
sort code date
bysort code: egen avg_ind=mean( ind)
save ../dta/last_6_8_2014, replace
